package com.example.luohao.myrecyclerviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zhxu.recyclerview.base.ViewHolder;
import com.zhxu.recyclerview.section.SectionRVAdapter;
import com.zhxu.recyclerview.section.StatelessSection;

import java.util.ArrayList;

/**
 * 项目名称：MyRecyclerViewDemo
 * 类描述：不同条目组实现
 * 创建人：luohao
 * 创建时间：2018/1/31 23:28
 * 修改人：luohao
 * 修改时间：2018/1/31 23:28
 * 修改备注：
 */

public class Activity3 extends AppCompatActivity {

    private RecyclerView mRV1;
    private ArrayList<String> str_list1;
    private ArrayList<String> str_list2;
    private ArrayList<String> str_list3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_recycler);
        mRV1 = this.findViewById(R.id.mRV1);
        mRV1.setLayoutManager(new LinearLayoutManager(this));
        SectionRVAdapter sectionRVAdapter = new SectionRVAdapter(this);
        str_list1 = new ArrayList<>();
        str_list2 = new ArrayList<>();
        str_list3 = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            str_list1.add("Section" + i);
            str_list2.add("Section" + 25 + i);
            str_list3.add("Section" + 25 * 2 + i);
        }
        sectionRVAdapter.addSection(new DataSection(this, "Section_1", str_list2));
        sectionRVAdapter.addSection(new DataSection(this, "Section_2", str_list1));
        sectionRVAdapter.addSection(new DataSection(this, "Section_3", str_list3));
        mRV1.setAdapter(sectionRVAdapter);
    }

    class DataSection extends StatelessSection {

        private final Context context;
        private final String section;
        private final ArrayList<String> str_list;

        public DataSection(Context context, String section, ArrayList<String> str_list) {
            super(R.layout.item_image_text, R.layout.item_common);
            this.context = context;
            this.section = section;
            this.str_list = str_list;
        }

        //不算标题单组内容条目数
        @Override
        public int getContentItemsTotal() {
            return str_list.size();
        }

        @Override
        public ViewHolder getItemViewHolder(View view, int viewType) {
            return new ViewHolder(context, view);
        }

        //条目赋值
        @Override
        public void onBindItemViewHolder(ViewHolder holder, int position) {
            String s = str_list.get(position);
            holder.setText(R.id.mTV_common, s);
        }

        //标题
        @Override
        public ViewHolder getHeaderViewHolder(Context context, View view) {
            return super.getHeaderViewHolder(context, view);
        }

        //标题赋值
        @Override
        public void onBindHeaderViewHolder(ViewHolder holder) {
            super.onBindHeaderViewHolder(holder);
            holder.setText(R.id.mTV_text, section);
        }
    }
}
